package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.apis.admin.AdChannelControllerApi;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.validate.HeimaValidatorAdd;
import com.heima.model.common.validate.HeimaValidatorUpdata;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理",tags = "频道管理-AdChannel",description = "频道管理API")
public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    private AdChannelService adChannelService;
    @PostMapping("/list")
    @Override
    @ApiOperation(value = "条件分页查询频道列表")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto dto) {
        return adChannelService.findByNameAndPage(dto);
    }
    @PostMapping("/save")
    @Override
    @ApiOperation(value = "新增频道")
    public ResponseResult insert(@RequestBody @Validated(HeimaValidatorAdd.class) AdChannel adChannel) {
        return adChannelService.insert(adChannel);
    }
    @Override
    @PutMapping("/update")
    @ApiOperation(value = "修改频道")
    public ResponseResult update(@RequestBody @Validated({HeimaValidatorAdd.class, HeimaValidatorUpdata.class   }) AdChannel adChannel) {
        return adChannelService.update(adChannel);
    }

    @Override
    @GetMapping( "/del/{id}")
    @ApiOperation(value = "删除频道")
    public ResponseResult deleteById(@PathVariable("id") Integer id) {
        return adChannelService.deleteById(id);
    }

    /**
     * 查询所有频道
     *
     * @return
     */
    @GetMapping("/channels")
    @Override
    public ResponseResult findAll() {
        List<AdChannel> list = adChannelService.list();
        return ResponseResult.okResult(list);
    }
}
